/*
 * LinkedMeFragment.java
 * business
 *
 * Created by SunDing on 2017/1/16.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.linkedme;

import android.os.Bundle;

import com.google.gson.Gson;
import com.microquation.linkedme.android.LinkedME;
import com.microquation.linkedme.android.util.LinkProperties;
import com.yidiandao.business.R;
import com.yidiandao.business.base.BaseFragment;
import com.yidiandao.business.data.bean.LinkedMeBean;
import com.yidiandao.business.utils.GsonProvider;
import com.yidiandao.business.utils.JumpUtils;
import com.yidiandao.business.utils.UIUtils;

import java.util.HashMap;

/** 处理linkedMe sdk返回的参数 并作出操作. */
public class LinkedMeFragment extends BaseFragment implements LinkedMeContract.View {

    private LinkedMeContract.Presenter mPresenter;

    public static LinkedMeFragment getInstance() {
        LinkedMeFragment fragment = new LinkedMeFragment();
        return fragment;
    }

    @Override
    public void setPresenter(LinkedMeContract.Presenter presenter) {
        mPresenter = presenter;
    }

    @Override
    public String getPagerTag() {
        return LinkedMeFragment.class.getSimpleName();
    }

    @Override
    public void showToast(String text) {
        UIUtils.showToast(text);
    }

    @Override
    protected void initViews() {
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_linkedme;
    }

    /**
     * 解析深度链获取跳转参数，开发者自己实现参数相对应的页面内容
     *
     * <p>通过LinkProperties对象调用getControlParams方法获取自定义参数的HashMap对象, 通过创建的自定义key获取相应的值,用于数据处理。
     */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (mActivity.getIntent() != null) {
            //获取与深度链接相关的值
            LinkProperties linkProperties =
                    mActivity.getIntent().getParcelableExtra(LinkedME.LM_LINKPROPERTIES);
            if (linkProperties != null) {
                /*  Log.i("LinkedME-Demo", "Channel " + linkProperties.getChannel());
                Log.i("LinkedME-Demo", "control params " + linkProperties.getControlParams());
                Log.i("LinkedME-Demo", "link(深度链接) " + linkProperties.getLMLink());
                Log.i("LinkedME-Demo", "是否为新安装 " + linkProperties.isLMNewUser());*/
                //获取自定义参数封装成的hashmap对象,参数键值对由集成方定义
                HashMap<String, String> hashMap = linkProperties.getControlParams();

                Gson gson = GsonProvider.getGson();
                String json = gson.toJson(hashMap);
                LinkedMeBean bean = gson.fromJson(json, LinkedMeBean.class);

                JumpUtils.clickLinked(mActivity, bean);
            }
        }
        mActivity.finish();
    }
}
